package com.iamteer.b_data;

import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
/**
 * 这种方式跟方式 2 本质上一样，但当方法比较多时，方式二要重复好多次。
 * 更优的方式是封装一个 BaseAction，继承 ActionSupport 并实现这几个接口
 * @author Chuck
 *
 */
public class DataAction1 extends ActionSupport implements RequestAware, SessionAware, ApplicationAware{
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;
	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	@Override
	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}
	
	@Override
	public String execute() throws Exception {
/*		
		// 1. 请求数据封装
		// 2. 调用 Service 处理数据
		// 3. 数据保存到域中
		//方式 1：直接拿到 ServletAPI
		
  		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		ServletContext application = ServletActionContext.getServletContext();
		
		request.setAttribute("request_data", "request");
		session.setAttribute("session_data", "session");
		application.setAttribute("application_data", "application");

		
		// 方式 2：通过 ActionContext 类
		// [解耦的方式实现对数据的操作]
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> request = ac.getContextMap();
		Map<String, Object> session = ac.getSession();
		Map<String, Object> application = ac.getApplication();
		
		request.put("request_data", "request_map");
		session.put("session_data", "session_map");
		application.put("application_data", "application_map");
*/		
		request.put("request_data", "request_map_Aware");
		session.put("session_data", "session_map_Aware");
		application.put("application_data", "application_map_Aware");
		
		return SUCCESS;
	}


}
